<template>
  <div style="margin-right:50px;">
    <el-form :model="form" status-icon :rules="rules" ref="form" label-width="180px" class="demo-ruleForm" 
      style="margin-top: 50px;" size="mini">
      <el-row>
        <el-col :span="8">
          <el-form-item label="产品组合" prop="t8_sys_portfol_id">
              <el-select v-model="form.t8_sys_portfol_id" @change="t8_sys_portfol_id_change_fnc" placeholder="请选择产品组合">
                <el-option
                  v-for="item in dict.t8_sys_portfol_id"
                  :key="item.value"
                  :label="item.text"
                  :value="item.value">
                </el-option>
              </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="产品/SPV" prop="t8_prod_info_id">
            <el-select v-model="form.t8_prod_info_id" placeholder="请选择产品/SPV"
              @change="t8_prod_info_id_change_fnc">
              <el-option
                v-for="item in dict.t8_prod_info_id"
                :key="item.value"
                :label="item.text"
                :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="产品/SPV余额(元)" prop="prod_principalbln">
            <el-input v-model="form.prod_principalbln" disabled></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="交易日期:" prop="tradedate">
            <el-date-picker
              v-model="form.tradedate"
              type="date"
              class="venus-input-middle venus-input-date"
              value-format="yyyyMMdd"
            />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="交易对手:" prop="t8_oside_basei_id">
            <el-select v-model="form.t8_oside_basei_id" placeholder="请选择交易对手"
              @change="t8_oside_basei_id_change_fnc">
              <el-option
                v-for="item in dict.t8_oside_basei_id"
                :key="item.value"
                :label="item.text"
                :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="资产类别:" prop="t8_sys_adtype_id">
            <el-select v-model="form.t8_sys_adtype_id" placeholder="请选择资产类别"
              @change="t8_sys_adtype_id_change_fnc">
              <el-option
                v-for="item in dict.t8_sys_adtype_id"
                :key="item.value"
                :label="item.text"
                :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="资产代码:" prop="ftool_code">
            <el-select v-model="form.ftool_code" placeholder="请选择资产代码" 
              @change="ftool_code_change_fnc">
              <el-option
                v-for="item in dict.ftool_code"
                :key="item.value"
                :label="item.text"
                :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="资产名称:" prop="ftool_name">
            <el-input v-model="form.ftool_name" disabled></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="到期日:" prop="end_date">
            <el-input v-model="form.end_date" disabled></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="交易方向:" prop="direction">
            <el-select v-model="form.direction" disabled>
              <el-option
                v-for="item in dict.direction"
                :key="item.value"
                :label="item.text"
                :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="会计分类:" prop="account_type">
            <el-select v-model="form.account_type">
              <el-option
                v-for="item in dict.account_type"
                :key="item.value"
                :label="item.text"
                :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="交易金额（元）:" prop="grossamount">
            <el-input v-model="form.grossamount"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="备注:" prop="note">
            <el-input v-model="form.note"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
        <el-form-item label="资金账号:" prop="account_code">
          <el-select v-model="form.account_code" placeholder="请选择资金账号"
            filterable
            clearable
            @change="account_code_change_fnc">
            <el-option
              v-for="item in dict.account_code"
              :key="item.account_code"
              :label="item.account_code"
              :value="item.account_code">
            </el-option>
          </el-select>
        </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="资金账户名称:" prop="account_name">
            <el-input v-model="form.account_name" disabled></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="开户行名称:" prop="opening_account_name">
            <el-input v-model="form.opening_account_name" disabled></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="开户行行号:" prop="opening_account_num">
            <el-input v-model="form.opening_account_num" disabled></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
  </div>
</template>
<script>
  import * as kk from '@/utils/k-utils'
  export default {
    name:"trade_fund_fh",
    props:{
        parent:Object  //父组件this
    },
    data() {
      return {
        //数据字典
        dict:{
          t8_sys_portfol_id:[],
          t8_prod_info_id:[],
          t8_oside_basei_id:[],
          t8_sys_adtype_id:[],
          ftool_code:[],
          direction:[
            {value:1,text:'收息'}
          ],
          account_type:[
            {value:3,text:'可供出售资产'},
            {value:5,text:'应收款项类资产'}
          ],
          account_code:[]
        },
        //form表单
        form:{
          t8_sys_portfol_id:'',
          t8_prod_info_id:'',
          prod_principalbln:'',
          tradedate:'',
          t8_oside_basei_id:'',
          t8_sys_adtype_id:'',
          ftool_code:'',
          ftool_name:'',
          end_date:'',
          direction:1,
          account_type:'',
          grossamount:'',
          dealtype:38,
          note:'',
          account_code:'',
          account_name:'',
          opening_account_name:'',
          opening_account_num:''
        },
        rules:{
          t8_sys_portfol_id:[
            {required: true, message: '请输入产品组合', trigger: 'change'}
          ],
          t8_prod_info_id:[
            {required: true, message: '请输入产品/SPV', trigger: 'change'}
          ],
          tradedate:[
            {required: true, message: '请输入交易日期', trigger: 'change'}
          ],
          t8_oside_basei_id:[
            {required: true, message: '请输入交易对手', trigger: 'change'}
          ],
          t8_sys_adtype_id:[
            {required: true, message: '请输入资产类别', trigger: 'change'}
          ],
          ftool_code:[
            {required: true, message: '请输入资产代码', trigger: 'change'}
          ],
          account_type:[
            {required: true, message: '请输入会计分类', trigger: 'change'}
          ],
          account_code:[
            {required: true, message: '请输入资金账号', trigger: 'change'}
          ],
          grossamount:[
            {required: true, message: '请输入交易金额', trigger: 'blur'},
            {validator:this.validator_money,trigger: 'blur'}
          ]
        }
      };
    },
    mounted:function(){
      //加载投组下拉框
      kk.k_load_exeid('public','selectPortfol').then(rep=>{
        this.dict.t8_sys_portfol_id=rep.rows;
      });
      //加载交易对手下拉框
      kk.k_load_exeid('public','selectOSIDE').then(rep=>{
        this.dict.t8_oside_basei_id=rep.rows;
      });
    },
    methods: {
      //校验输入金额
      validator_money(rule,value,callback){
        if(value < 0.01){
          return new callback(new Error("输入金额必须大于0"));
        }
        callback();
      },
      //投资组合变动
      t8_sys_portfol_id_change_fnc(){
        kk.k_load_exeid('public','selectProd',{t8_sys_portfol_id:this.form.t8_sys_portfol_id,tradedate:this.form.tradedate}).then(rep=>{
          this.dict.t8_prod_info_id=rep.rows;
        });
      },
      //理财产品变动
      t8_prod_info_id_change_fnc(){
        if(this.form.t8_prod_info_id != ""){
          kk.k_load_exeid('data','getProdBalance',{t8_prod_info_id:this.form.t8_prod_info_id,queryDate:this.form.tradedate}).then(rep=>{
            this.form.prod_principalbln=rep.rows[0].prod_balance;
          });
          //加载账号信息
          this.account_code_load_fnc();
        }
      },
      t8_oside_basei_id_change_fnc(){
        if(this.form.t8_oside_basei_id != ""){
          //加载资产类别
          kk.k_load_exeid('custom','M830EQ004',{T8_SYS_PORTFOL_ID:this.form.t8_sys_portfol_id,tradedate:this.form.tradedate,t8_oside_basei_id:this.form.t8_oside_basei_id}).then(rep=>{
            this.dict.t8_sys_adtype_id=rep.rows;
          });
        }
      },
      //资产类别
      t8_sys_adtype_id_change_fnc(){
        //加载资产代码
          kk.k_load_exeid('custom','M830EQ006',{direction:this.form.direction,dealtype:this.form.dealtype,tradedate:this.form.tradedate,
            t8_sys_adtype_id:this.form.t8_sys_adtype_id,t8_sys_portfol_id:this.form.t8_sys_portfol_id,t8_oside_basei_id:this.form.t8_oside_basei_id}).then(rep=>{
            this.dict.ftool_code=rep.rows;
          });
      },
      //资产代码
      ftool_code_change_fnc(){
        kk.k_load_exeid('custom','M8252SleteCode',{ftool_code:this.form.ftool_code,t8_sys_adtype_id:this.form.t8_sys_adtype_id}).then(rep=>{
          this.form.ftool_name=rep.rows[0].ftool_name;
          this.form.end_date=rep.rows[0].enddate;
        });
      },
      account_code_load_fnc(){
        kk.k_load_exeid('public','selectAccByCondition',{t8_prod_info_id:this.form.t8_prod_info_id,broker_code:''}).then(rep=>{
          this.dict.account_code=rep.rows;
        });
      },
      account_code_change_fnc(){
        this.dict.account_code.forEach(element => {
          if(element.account_code == this.form.account_code){
            this.form.account_name=element.account_name;
            this.form.opening_account_name=element.opening_account_name;
            this.form.opening_account_num=element.opening_account_num;
          }
        });
      },
      //获取表单参数
      getFormParams(formName){
        let isValidate=false;
        this.$refs[formName].validate((valid) => {
          isValidate=valid;
        });
        if(isValidate){
          return this.form;
        }else{
          return false;
        }
      },
      //重置表单
      resetForm(formName) {
        this.$refs[formName].resetFields();
      }
    }
  }
</script>

<style scoped>
</style>
